<?php
class CStock
{
	const API_URL = "http://stock.gogoalshop.com";
	const SITE = "newjerseysoccer.net";
	
	public static function getStock($sku)
	{
		$skus = explode(',', $sku);
		
		$url = self::API_URL . '/get.php?sku=' . trim($skus[0]);
		$content = '';
		$i = 10;
		while (empty($content) && $i > 0) {
			$content = self::curlGet($url);
			$i--;
		}
				
		$arr = json_decode($content, true);
		$result = array();
		if ($arr) {
			$tmp = array();
			$tmpStock = array();
			$stocknum = 0;
			foreach ($arr as $v) {
				if($arr[0]['is_forlong']==1 || $v['value']>0){
					$tmp[] = $v['size'];
				}
				$tmpStock[] = $v['value'];
				$stocknum += $v['value'];				
			}
						
			$result['is_forlong'] = $arr[0]['is_forlong'];
			$result['size'] = $tmp;
			$result['stock'] = $tmpStock;
			$result['stocknum'] = $stocknum;			
			return $result;
		}		
		return 0;
	}
	
	public static function update($orderId, $flag = 'sub')
	{
		$data = self::dataFilter($orderId);
		if(!$data) {
			return;
		}
		$result = array();
		$result['site'] = self::SITE;
		$result['data'] = $data;
		$result['order_id'] = $orderId;
		$result['flag'] = $flag;
		$url = self::API_URL . '/update.php';
		$content = self::curlPost($url, array('d'=>json_encode($result)));
		return $content;
	}
	
	public static function curlPost($url, $data)
	{
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
		$contents = curl_exec($ch);
		curl_close($ch);
		return $contents;
	}
	
	public static function curlGet($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$r = curl_exec($ch);
		curl_close($ch);
		return $r;
	}
	
	public static function dataFilter($orderId)
	{
		$order = new COrder();
		$orderRow = $order->getDetail($orderId);
		$arr = unserialize(stripslashes($orderRow[0]['order_string']));
		if(!$arr){
			$arr = unserialize($orderRow[0]['order_string']);
		}
		$item = new CItem();
		$data = array();
		foreach($arr as $v) {
			$itemArray = $item->getDetail($v['itemid'],'product');
			if ($itemArray[0]['sku']) { // 没有sku的产品不管
				$patternSize = $v['buy_pattern_size'];
		    	$patternSize = preg_replace('/<font style=\'color: red\'>[This size sold out, please wait for 5-7 days]<\/font>,/', '', $patternSize);
		    	$arr = explode(',', trim(trim($patternSize), ','));   	
		    	
		    	foreach ($arr as $_v) {
		    		$skus = explode(',', $itemArray[0]['sku']);
		    		foreach ($skus as $__v) {
	    				$data[trim($__v)][] = explode(':', trim($_v));
		    		}
	    		}
			}
			//配件
    		$acc = $v['accessory'];    		
    		if ($acc) {
    			preg_match_all('/Name:.*?pd-(\d+)\..*?Size:(.*?),Quantity:(\d+)/i', $v['accessory'], $match);
    			if (!empty($match[0])) {
    				$i = 0;
    				for($i = 0; $i<count($match[0]); $i++) {
    					$itemArray = $item->getDetail($match[1][$i],'product');
    					if ($itemArray[0]['sku']) { // 没有sku的产品不管
	    					$skus = explode(',', $itemArray[0]['sku']);
				    		foreach ($skus as $__v) {
			    				$data[trim($__v)][] = array($match[2][$i], $match[3][$i]);
				    		}
    					}
    				}
    			}
    		}
		}
		return $data;
	}
}